/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2024-10-28
 * Time:21:37
 */
public class Test {
    public static void main1(String[] args) {
        MySingleList mySingleList=new MySingleList();
        //mySingleList.creatList();
        mySingleList.addFirst(12);
        mySingleList.addFirst(23);
        mySingleList.addFirst(34);
        mySingleList.addFirst(23);
        mySingleList.addFirst(56);
        System.out.println();
        //System.out.println(mySingleList.size());
        //System.out.println(mySingleList.contains(13));
        //mySingleList.addLast(7);
        //mySingleList.addIndex(3,99);
        //mySingleList.remove(12);
        //mySingleList.removeAllKey(23);
        //mySingleList.clear();
        MySingleList.ListNode ret=mySingleList.FindKthToTail(3);
        System.out.println(ret.val);
        //mySingleList.display();


        //System.out.println("1234");

    }
        public static MySingleList.ListNode getIntersectionNode(MySingleList.ListNode headA,
                                                                MySingleList.ListNode headB) {
            //分别求两个链表的长度
            int lenA=0;
            int lenB=0;
            MySingleList.ListNode pL=headA;//假设pL所指的长度是最长的链表
            MySingleList.ListNode pS=headB;//假设pS所指的长度是最短的链表
            while(pL!=null){
                lenA++;
                pL=pL.next;
            }
            while(pS!=null){
                lenB++;
                pS=pS.next;
            }
            pL=headA;
            pS=headB;
            int len=lenA-lenB;
            if(len<0){
                pL=headB;
                pS=headA;
                len=lenB-lenA;
            }
            //保证了pL一定时最长的链表pS一定时最短的链表，len为正数
            //先让最长的链表先走len步
            while(len!=0){
                pL=pL.next;
                len--;
            }
            //两个同时走,就是相遇的点
            while(pL!=pS){
                pL=pL.next;
                pS=pS.next;
            }
            return pL;
        }

        private static void createIntersect(MySingleList.ListNode headA,
                                            MySingleList.ListNode headB){
        headB.next.next=headA.next.next;
        }
        //判断两个链表相交
    public static void main2(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addFirst(12);
        mySingleList1.addFirst(23);
        mySingleList1.addFirst(34);
        mySingleList1.addFirst(23);
        mySingleList1.addFirst(56);
        mySingleList1.display();
        System.out.println();
        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addFirst(8);
        mySingleList2.addFirst(15);
        mySingleList2.addFirst(30);
        mySingleList2.addFirst(40);
        mySingleList2.addFirst(78);
        mySingleList2.display();
        System.out.println();
        createIntersect(mySingleList1.head,mySingleList2.head);
        MySingleList.ListNode ret=getIntersectionNode(mySingleList1.head,
                mySingleList2.head);
        System.out.println(ret.val);
    }
    public static void main3(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addFirst(12);
        mySingleList1.addFirst(23);
        mySingleList1.addFirst(34);
        mySingleList1.addFirst(23);
        mySingleList1.addFirst(56);
        mySingleList1.display();
        System.out.println();
        mySingleList1.creatLoop();
        System.out.println(mySingleList1.hasCycle());

    }

    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addFirst(12);
        mySingleList1.addFirst(23);
        mySingleList1.addFirst(34);
        mySingleList1.addFirst(45);
        mySingleList1.addFirst(56);
        mySingleList1.display();
        mySingleList1.reversePrintList();
    }
}
